package com.wnxy.controller;


import com.wnxy.annotation.Validate;
import com.wnxy.dto.*;
import com.wnxy.model.Manager;
import com.wnxy.service.ManagerService;
import com.wnxy.util.BeanCopyUtil;
import com.wnxy.util.GetRequestUtil;
import com.wnxy.vo.*;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author alice
 * @since 2021-10-25
 */
@RestController
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    private ManagerService managerService;
    @Autowired
    private RedisTemplate<String, Object> rt;
    @PostMapping("/save")
    public Result save(@RequestBody RegisterVo registerVo) {
        System.out.println(registerVo);
   LoginDto ld= managerService.saveManager(registerVo);
        return Result.getInstance().setFlag(true)
                .setStatusCode(200)
                .setMessage("保存").setData(ld);
    }

    @GetMapping("/loginPage")
    public Result loginPage() {

        return Result.getInstance().setFlag(true)
                .setStatusCode(201)
                .setMessage("请先登录").setData(null);
    }
    @PostMapping("/query")
    public Result query(@RequestBody QueryPageVo queryPageVo) {
        PageRoleDto pageDto = managerService.queryManager(queryPageVo);
        return Result.getInstance().setFlag(true)
                .setStatusCode(200)
                .setMessage("查询成功").setData(pageDto);
    }
    @PostMapping("/changePhoto")
    public Result changePhoto(@RequestBody PhotoVo photoVo) {
        LoginDto manager=managerService.changePhoto(photoVo);
        return Result.getInstance().setFlag(true)
                .setStatusCode(200)
                .setMessage("修改成功").setData(manager);
    }
    @PostMapping("/qqbind")
    public Result qqbind(@RequestBody QQbindVo qqbindvo) {
        System.out.println(qqbindvo);
        LoginDto manager=managerService.qqbind(qqbindvo);
        return Result.getInstance().setFlag(true)
                .setStatusCode(200)
                .setMessage("修改成功").setData(manager);
    }
//    @Validate
    @GetMapping("/logout")
    public Result logout() {
        GetRequestUtil.getSession().removeAttribute("loginManager");
        return Result.getInstance().setFlag(true)
                .setStatusCode(StatusCode.SUCCESS.getCode())
                .setMessage("退出成功").setData(null);
    }
    @GetMapping("/sendCode/{phone}")
    @ApiImplicitParam("电话号码")
    public Result sendCode(@PathVariable("phone") String phone) {
        LoginDto loginDto = managerService.sendCode(phone);
        return Result.getInstance().setFlag(true)
                .setStatusCode(StatusCode.SUCCESS.getCode())
                .setMessage("发送验证码成功").setData(loginDto);
    }
//    @PostMapping("/loginByPhone")
    public Result loginByPhone(@RequestBody LoginByPhoneVo loginByPhoneVo) throws IllegalAccessException {
        LoginDto loginDto = managerService.loginByPhone(loginByPhoneVo);
        return Result.getInstance().setFlag(true)
                .setStatusCode(StatusCode.SUCCESS.getCode())
                .setMessage("登陆成功").setData(loginDto);
    }
    @GetMapping("/loadPower")
    public Result loadPower(){

//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        System.out.println(principal);
//        ManagerDetail managerDetail = (ManagerDetail) principal;
        managerService.loadRolePath();
        return Result.getInstance().setFlag(true)
                .setStatusCode(StatusCode.SUCCESS.getCode());
    }
    @GetMapping("/text")
    public Object text(){
        managerService.loadRolePath();
       return 1;
    }

}

